package com.itheima.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import com.itheima.dto.CartDto;
import com.itheima.dto.GoodsDetailDto;
import com.itheima.dto.ReginDto;
import com.itheima.entity.Cart;
import com.itheima.mapper.CartMapper;
import com.itheima.service.CartService;

/**
 * @author ：yuan_he_song
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Override
    public List<CartDto> list(Integer companyId) {
        List<CartDto> list = cartMapper.findCartList(companyId);
        return convertList(list);
    }

    private  List<CartDto> convertList(List<CartDto> list) {
        Map<Integer, List<CartDto>> map = new HashMap<>();
        for (CartDto cartDto : list) {
            List<CartDto> cartList = map.get(cartDto.getSalesId());
            if (ObjectUtils.isEmpty(cartList)){
                cartList = new ArrayList<>();
                cartList.add(cartDto);
                map.put(cartDto.getSalesId(), cartList);
            }else {
                cartList.add(cartDto);
            }
        }

        List<CartDto> result = new ArrayList<>();
        Set<Integer> ids = map.keySet();
        for (Integer salesId : ids) {
            CartDto cartDto = new CartDto();
            List<CartDto> cartDtoList = map.get(salesId);
            // TODO 是否建立了采购关系
            cartDto.setSalesId(salesId);
            if (salesId % 2 == 0){
                cartDto.setBuild(true);
            }else {
                cartDto.setBuild(false);
            }
            cartDto.setSalesName(cartDtoList.get(0).getSalesName());
            List<GoodsDetailDto> carts = getList(salesId, 1);
            cartDto.setCartList(carts);
            result.add(cartDto);
        }
        return result;
    }

    private List<GoodsDetailDto> getList(Integer salesId, Integer companyId) {
        List<GoodsDetailDto> redult = new ArrayList<>();
        List<Cart> carts = cartMapper.getListBySalesId(salesId,companyId);
        for (Cart cart : carts) {
            GoodsDetailDto dto = new GoodsDetailDto();
            BeanUtils.copyProperties(cart, dto);
            // TODO 购物车列表字段查询赋值
            if (dto.getSalesId() == 1){
                dto.setHideButton(true);
                dto.setReson("盛星的商品置灰！");
            }else {
                dto.setHideButton(false);
                dto.setReson("非盛星可以点击！");
            }
            redult.add(dto);
        }
        return redult;
    }

    public static void main(String[] args) {
        int buyNum = 600; // 购买的数量 0:没有买意向商品
        List<ReginDto> list = getList(false);
        double result = getPrice(buyNum, list);
        System.err.println("结果是：>>>>>>>> " + result);
    }

    private static double getPrice(int buyNum, List<ReginDto> list) {
        double a = 2.0; // 拼单活动价
        double b = 1.8; // 意向活动价
        double f = 10; // 出厂价
        if (buyNum < 1){
          return a;  // 没有买意向商品
        }
        if(CollectionUtils.isEmpty(list)) {
            return b; // 意向活动价
        }
        double result = 0;
        for (int i = 0; i < list.size(); i++) {
            ReginDto reginDto = list.get(i);
            if (i == 0){
                if (buyNum < reginDto.getNum()){ // 最低都没达到
                    result =  b;  // 返回意向活动价
                    break;
                }
            }
            else if (i != list.size() - 1) {
                if (buyNum < reginDto.getNum()){
                    ReginDto last = list.get(i - 1);
                    result =  f * last.getDiscount();  // 返回上一阶梯活动价
                    break;
                } else {
                    ReginDto next = list.get(i + 1);
                    if (buyNum < next.getNum()){
                        result = f * reginDto.getDiscount(); // 本阶梯的价格
                        break;
                    }
                }
            }
            else { // 最后一个梯度
                if (buyNum < reginDto.getNum()){
                    ReginDto last = list.get(i - 1);
                    result =  f * last.getDiscount();  // 返回上一阶梯活动价
                } else {
                    result = f * reginDto.getDiscount();
                }
            }


        }
        return result;
    }

    private static List<ReginDto> getList(boolean result) {
        List<ReginDto> list = new ArrayList<>();
        if (result){
            return list;
        }

        ReginDto r1 = new ReginDto();
        r1.setNum(100);
        r1.setDiscount(0.9);

        ReginDto r2 = new ReginDto();
        r2.setNum(300);
        r2.setDiscount(0.8);

        ReginDto r3 = new ReginDto();
        r3.setNum(500);
        r3.setDiscount(0.5);

        list.add(r1);
        list.add(r2);
        list.add(r3);

        return list;
    }
}
